#include <jni.h>

#include <stdio.h>
#include <sys/types.h>
#include <libusb.h>
#include <string.h>


//linux-command >> lsusb -v 
//you will get next info
#define USB_VID             0x20d1
#define USB_PID             0x7008
#define EP2ADDR             0x02                //Write端口2地址，通道2 
#define EP3ADDR             0x86                //Read 端口3地址，通道3
#define USB_TIMEOUT         10000               //传输数据的时间延迟
#define MAX_BYTE            10240000            //DL888 MAX_BYTE 10M

libusb_device_handle *dev_handle = NULL; 
libusb_context *ctx = NULL;   
char file_content[MAX_BYTE];

int init(){
    int ret = 1;
    ret = libusb_init(NULL);  
    if(ret < 0) {
        fprintf(stderr, "failed to initialise libusb\n");
        return 0;    
    }

    dev_handle = libusb_open_device_with_vid_pid(NULL, USB_VID, USB_PID);
    if(dev_handle == NULL){
        perror("Cannot open device\n");
        return 0;
    }else{
        printf("Device Opened\n");
    }

    if(libusb_kernel_driver_active(dev_handle, 0) == 1) {  
        printf("Kernel Driver Active\n");
        if(libusb_detach_kernel_driver(dev_handle, 0) == 0){ 
            printf("Kernel Driver Detached!\n");
        }
    }   

    ret = libusb_claim_interface(dev_handle, 0); 
    if(ret < 0) {
        perror("Cannot Claim Interface\n");
        return 0;
    }
    return 1;
}

int read_prn_file(char *filename){
    int len;
    FILE *f = fopen(filename,"rb");
    if(f==NULL){
        printf("error");
        return 0;
    }
    len = fread(file_content, 1, MAX_BYTE, f);
    fclose(f);
    fprintf(stderr, "%s: %d bytes\n", filename, len);
    return len;
}

void first_send(){
    char first[] = {0x1b,0x53};
    int transferred = 0;
    libusb_bulk_transfer(dev_handle, EP2ADDR, first, 2, &transferred, USB_TIMEOUT);
}

void realese(){
    int ret = 0;
    ret = libusb_release_interface(dev_handle, 0);
    if(ret != 0){
        printf("Cannot Released Interface!\n");
    }else{
        printf("Released Interface!\n");
    }
    libusb_close(dev_handle); 
    libusb_exit(ctx); 
}

void send_msg(int len){
        int ret = 1;
        int i = 0;
        int transferred = 0;
        //printf("fc = %s,len = %d\n",file_content,len);
        first_send();
        ret = libusb_bulk_transfer(dev_handle, EP2ADDR, file_content, len, &transferred, USB_TIMEOUT);
        //printf("ret = %d,trans = %d\n",ret,transferred);
        if(ret==0 && transferred==len){
            printf("write Successful!\n");
        }else{
            printf("write error!\n");
        }
}
/*
char* jstringToChar(JNIEnv *env, jstring jstr) {
    int length = (env)->GetStringLength(jstr);
    const jchar* jcstr = (env)->GetStringChars(jstr, 0);
    char* rtn = (char*) malloc(length * 2 + 1);
    int size = 0;
    size = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR) jcstr, length, rtn,
            (length * 2 + 1), NULL, NULL);
    if (size <= 0)
        return NULL;
    (env)->ReleaseStringChars(jstr, jcstr);
    rtn[size] = 0;
    return rtn;
}*/
/*
 * Class:     indi_walty_dl8888_dirver_dirver_DL8888
 * Method:    sendMsg
 * Signature: (Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_indi_walty_dl8888_1dirver_dirver_DL8888_sendMsg (JNIEnv *env, jobject obj, jstring msg){
    int len = 0;  
    char *filename = (char*)((*env)->GetStringUTFChars(env,msg,0));
    len = read_prn_file(filename);
    if(init() && len){
        send_msg(len);
        realese();
    }else{
        printf("config err!!\n");
        return -1;
    }
    (*env)->ReleaseStringUTFChars(env, msg, filename);
    return 0;
}

